package com.infore.airsupervise.ui.fragment;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.infore.airsupervise.R;
import com.infore.airsupervise.adapter.SelectPhotoAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;


/**
 * 选择照片组件
 * 依赖于galleryFinal、material、SelectPhotoRecyclerView适配器
 */
@SuppressLint("ValidFragment")
public class SelectPhotoFragment extends Fragment {

    private final int REQUEST_CODE_GALLERY = 1001;

    private int maxValue;       // 最多可选数量
    private int itemWidth;      // item高度

    private RecyclerView recyclerView;
    private SelectPhotoAdapter mAdapter;
    private List<PhotoInfo> selectedPhotos = new ArrayList<>();

    public SelectPhotoFragment() {
    }

    public SelectPhotoFragment(int maxValue, int itemWidth) {
        this.maxValue = maxValue;
        this.itemWidth = itemWidth;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_select_photo, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.select_photo_recycler_view);

        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        //设置适配器
        mAdapter = new SelectPhotoAdapter(selectedPhotos, itemWidth);
        recyclerView.setAdapter(mAdapter);

        // 设置监听
        mAdapter.setOnItemClickListener(new SelectPhotoAdapter.OnItemClickListener() {
            @Override
            public void onAddItemClick(View view) {
                // 打开相册
                selectInGallery();
            }
            @Override
            public void onDeleteItemClick(View view, int position) {
                // 删除操作
                selectedPhotos.remove(position);
                mAdapter.notifyDataSetChanged();
            }
        });

        return view;
    }

    /**
     * 打开相册
     */
    private void selectInGallery() {
        //带配置
        FunctionConfig config = new FunctionConfig.Builder()
                .setMutiSelectMaxSize(maxValue)
                .setSelected(selectedPhotos)
                .setEnablePreview(true)
                .setEnableCamera(true)
                .build();
        GalleryFinal.openGalleryMuti(REQUEST_CODE_GALLERY, config, mOnHanlderResultCallback);
    }

    /**
     * 选择照片结果回调
     */
    private GalleryFinal.OnHanlderResultCallback mOnHanlderResultCallback = new GalleryFinal.OnHanlderResultCallback() {
        @Override
        public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
            if (resultList == null) {
                return;
            }

            selectedPhotos.clear();
            selectedPhotos.addAll(resultList);
            mAdapter.notifyDataSetChanged();
        }

        @Override
        public void onHanlderFailure(int requestCode, String errorMsg) {
        }
    };

    /**
     * 外部获取已选照片
     * @return
     */
    public List<PhotoInfo> getSelectedPhotos() {
        return selectedPhotos;
    }

    /**
     * 重置照片集合
     */
    public void resetPhotos(){
        selectedPhotos.clear();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 此对象为static，手动释放
        mOnHanlderResultCallback = null;
    }
}
